<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="650px" @confirm="handleSubmit" @close="handleClose">
			<el-form ref="formRef" :model="formData" label-width="80px" :rules="formRules">
				<el-form-item label="类型" prop="type">
					<el-select v-model="formData.type" placeholder="请选择">
					    <el-option label="一般户" :value="1"></el-option>
						<el-option label="基本户" :value="2"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="户名" prop="pay_name">
					<el-input v-model="formData.pay_name" placeholder="请输入户名" clearable />
				</el-form-item>
				<el-form-item label="账号" prop="pay_account">
					<el-input v-model="formData.pay_account" placeholder="请输入账号" clearable />
				</el-form-item>
				<el-form-item label="开户行" prop="pay_bank">
					<el-input v-model="formData.pay_bank" placeholder="请输入开户行" clearable />
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="formData.remark" placeholder="请输入备注" clearable />
				</el-form-item>
				<el-form-item label="启用状态" prop="is_enable" required>
					<el-radio-group v-model="formData.is_enable">
						<el-radio :label="1">启用</el-radio>
						<el-radio :label="0">停用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
		</popup>
	</div>
</template>
<script lang="ts" setup>
	import type { FormInstance } from 'element-plus'
	import { payConfigAdd, payConfigEdit } from '@/api/finance';
	import Popup from '@/components/popup/index.vue'
	import { getModulesKey } from '@/router'

	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')
	const popupTitle = computed(() => {
		return mode.value == 'edit' ? '编辑账户' : '新增账户'
	})

	const componentsOptions = ref(getModulesKey())
	const querySearch = (queryString : string, cb : any) => {
		const results = queryString
			? componentsOptions.value.filter((item) =>
				item.toLowerCase().includes(queryString.toLowerCase())
			)
			: componentsOptions.value
		cb(results.map((item) => ({ value: item })))
	}
	const formData = reactive({
		id: '',
		//账户类型
		type: 1,
		//户名
		pay_name: '',
		//账号
		pay_account: '',
		//开户行
		pay_bank: '',
		//备注
		remark: '',
		//启用状态
		is_enable: 1
	})

	const formRules = {
		type: [
			{
				required: true,
				message: '请选择账户类型',
				trigger: 'blur'
			}
		],
		pay_name: [
			{
				required: true,
				message: '请输入户名',
				trigger: 'blur'
			}
		],
		pay_account: [
			{
				required: true,
				message: '请输入账号',
				trigger: 'blur'
			}
		],
		pay_bank: [
			{
				required: true,
				message: '请输入开户行',
				trigger: 'blur'
			}
		]
	}
	const menuOptions = ref<any[]>([])

	const handleSubmit = async () => {
		await formRef.value?.validate()
		let newFormData = JSON.parse(JSON.stringify(formData));
		mode.value == 'edit' ? await payConfigEdit(newFormData) : await payConfigAdd(newFormData)
		popupRef.value?.close()
		emit('success')
	}

	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}
	
	const setFormData = (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}
	}

	const getDetail = async (row : Record<string, any>) => {
		setFormData(row)
	}

	const handleClose = () => {
		emit('close')
	}

	defineExpose({
		open,
		setFormData,
		getDetail
	})
</script>

<style scoped lang="scss">
	// .dataTable {
	// 	width: 80%;
	// 	line-height: 40px;
	// 	text-align: left;
	// 	border-top: 1px solid #eaeaea;
	// 	border-left: 1px solid #eaeaea;
	// 	border-right: 1px solid #eaeaea;

	// 	tr {
	// 		// display: flex;

	// 		th {
	// 			flex: 1;
	// 			height: 40px;
	// 			padding: 0 16px;
	// 			font-weight: 500;
	// 			font-size: 12px;
	// 			color: #999999;
	// 			// background: #f9f8f8;
	// 			border-bottom: 1px solid #eaeaea;
	// 			border-right: 1px solid #eaeaea;
	// 		}

	// 		td {
	// 			flex: 1;
	// 			height: 40px;
	// 			padding: 0 16px;
	// 			font-size: 12px;
	// 			color: #666;
	// 			border-bottom: 1px solid #eaeaea;
	// 			border-right: 1px solid #eaeaea;
	// 		}
	// 	}
	// }
</style>